//
//  ModifyPwdController.m
//  easystudy
//
//  Created by kane on 15/6/8.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "ModifyPwdController.h"

@interface ModifyPwdController ()<UIAlertViewDelegate>
{
    __weak IBOutlet UITextField *_newPwdTF;
    __weak IBOutlet UITextField *_confirmPwdTF;
}

@end

@implementation ModifyPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Actions

- (IBAction)backBtnPressed:(id)sender {
    if (self.bIsForget) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)commitBtnPressed:(id)sender
{
    if ([_newPwdTF isFirstResponder] || [_confirmPwdTF isFirstResponder]) {
        [self.view endEditing:YES];
        return;
    }
    
    if (_newPwdTF.text.length == 0) {
        [self.view makeToast:@"请输入新密码"];
        return;
    }
    if (_confirmPwdTF.text.length == 0) {
        [self.view makeToast:@"请输入确认密码"];
        return;
    }
    if (![_confirmPwdTF.text isEqualToString:_newPwdTF.text]) {
        [self.view makeToast:@"确认密码与新密码不一致，请重新输入"];
        return;
    }
    NSDictionary *params = @{@"mobile":self.mobile,@"password":_newPwdTF.text};
    [WebService modifyPwd:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"result"] intValue] == 1) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:result[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex) {
        [self backBtnPressed:nil];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
